/*
************************************************************************************************************************
	Creates yearly data sets mia_con_`year' for years 2000-2006 with information on:
	- monthly employment: m3_1-m3_12
	- monthly earnings: l2_1-l2_12

	Registers: Variables (Years) 
	- MIA: pnr (2000-2006)
	- CONESR: pnr, senr, ansfra, anstil, helarkod, lonblb (2000-2005)
	- RAS: pnr, senr, ansfra, anstil, helarkod, loenblb (2006)

************************************************************************************************************************
*/

*******************************************
/*
* Combine monthly data sets mia_pnrYYYYmMM to create yearly data sets mia_pnr_`year' for years 2000-2006.
* Create 12 variables (m_1-m_12) for number of monthly employment observations for each indivual in each firm.  
*/
*******************************************

forvalues year=2000/2006 {
clear
local m_txt_list "01 02 03 04 05 06 07 08 09 10 11 12"
local i=0

foreach month_txt in `m_txt_list' {
g year=`year'
tempfile `year'm`month_txt'
use pnr senr using "$rawdata/mia_pnr`year'm`month_txt'", clear
local i=`i'+1
g m_`i'=1
drop if pnr=="."
destring pnr, replace

g e=1
bysort pnr senr: g se_num=sum(e)
keep if se_num==1
capture drop se_num

save ``year'm`month_txt''
}

clear
set obs 1
g pnr=.
g senr="."
foreach month_txt in `m_txt_list' {
capture drop _merge
sort pnr senr
merge 1:1 pnr senr using ``year'm`month_txt''

}
drop if pnr==.
drop if senr=="."
capture drop _merge
save "$temp/mia_`year'", replace
}

*******************************************
* Merge mia_pnr_`year' with conesr`year' (or ras`year' if year==2006) for each individual in each firm.
*******************************************

forvalues year=2000/2006 {

if `year'<2006  use pnr senr ansfra anstil helarkod lonblb  using "$rawdata/conesr`year'", clear
if `year'>=2006 use pnr senr ansfra anstil helarkod loenblb using "$rawdata/ras`year'", clear
if `year'>=2006 rename (senr`year' ansfra`year' anstil`year' helarkod`year' loenblb`year' ) (senr ansfra anstil helarkod lonblb)

drop if senr=="."

if `year'>=2006 tostring ansfra, replace
capture drop con_fra_month
g con_fra_month=substr(ansfra,1,length(ansfra)-2)
destring con_fra_month, replace
g con_fra_day=substr(ansfra,length(ansfra)-1,2)
destring con_fra_day, replace

if `year'>=2006 tostring anstil, replace
capture drop con_til_month
g con_til_month=substr(anstil,1,length(anstil)-2)
destring con_til_month, replace
g con_til_day=substr(anstil,length(ansfra)-1,2)
destring con_til_day, replace

capture drop _merge
merge m:1 pnr senr using "$temp/mia_`year'" , keep(match using)

*******************************************
* Define number of observation per individual per firm.
*******************************************

capture drop e
g e=1
capture drop pnrse_num
bysort pnr senr: g pnrse_num=sum(e)

*******************************************
* Define sum of earnings per individual per firm.
*******************************************
capture drop lonsum_se
sort pnr senr pnrse_num
by pnr senr: g lonsum_se=sum(lonblb)

*******************************************
* Keep one observation per individual per firm.
*******************************************
gsort pnr senr -pnrse_num
capture drop pnrse_num2
by pnr senr: g pnrse_num2=sum(e)
keep if pnrse_num2==1

capture drop pnrse_num pnrse_num2

*******************************************
* Set variables (m_1-m_12) = 1 if any observed employment for each individual in each firm.
* Count months of employment for each individual in each firm.
*******************************************
capture drop ans_mia_md
g ans_mia_md=0
forvalues month =1/12 {
replace m_`month'=1 if m_`month'>0 & m_`month'!=.
replace ans_mia_md=ans_mia_md+1 if m_`month'==1
}

*******************************************
* Calculate average monthly earning per month of employment for each individual in each firm.
*******************************************
g lon_md=lonsum_se/ans_mia_md

*******************************************
* Generate variable l_1-l_12 with avarage montly earning in months with employment
*******************************************
forvalues month =1/12 {
g l_`month'=0
replace l_`month'=m_`month'*lon_md if m_`month'!=.
}

*******************************************
* Generate variable m3_1-m3_12 with maximum montly employment (0/1)
* Generate variable l2_1-l2_12 with maximum montly earning in months with employment
*******************************************
forvalues month =1/12 {
capture drop m3_`month'
bysort pnr: egen m3_`month'=max(m_`month')
capture drop l2_`month'
bysort pnr: egen l2_`month'=max(l_`month') 
}

*******************************************
* Define number of observations per individual.
* Keep one observation per individual.
* Save mia_con_`year'.
*******************************************
bysort pnr: egen lonsum2=sum(lonsum_se)

capture drop pnr_num
bysort pnr: g pnr_num=sum(e)
keep if pnr_num==1

capture drop _merge
save "$temp/mia_con_`year'", replace
}